#include "..\StdAfx.h"
#include "Mountain.h"

DXMesh Mountain::s_mesh;

HRESULT Mountain::CreateMesh(LPDIRECT3DDEVICE9 pd3ddev9)
{
	HRESULT hr = D3DXCreateBox(pd3ddev9, 20.0f, 20.0f, 20.0f, &s_mesh.m_pMesh, &s_mesh.m_pAdjacency);

	if (hr == S_OK)
	{
		s_mesh.m_pMesh->OptimizeInplace(
			D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
			( DWORD* )s_mesh.m_pAdjacency->GetBufferPointer(), NULL, NULL, NULL );
	}

	LPD3DXMESH pNewMesh = NULL;

	// Optimize the mesh for performance
	if( FAILED( hr = s_mesh.m_pMesh->OptimizeInplace(
		D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
		( DWORD* )s_mesh.m_pAdjacency->GetBufferPointer(), NULL, NULL, NULL ) ) )
	{
		s_mesh.m_pAdjacency->Release();
		return hr;
	}

	/*
	if ( SUCCEEDED( s_mesh.m_pMesh->CloneMeshFVF(s_mesh.m_pMesh->GetOptions(),
		(D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1),
		pd3ddev9, &pNewMesh) ) )
	{
		s_mesh.m_pMesh->Release();
		s_mesh.m_pMesh = pNewMesh;

		_TRACE(_T("New Vertex FVF: 0x%.8X, have %u number of vertexes\n"), 
			s_mesh.m_pMesh->GetFVF(), 
			s_mesh.m_pMesh->GetNumVertices());

		LPVERTEXINFO_TEX1 pVertexInfo = NULL;

		// lock vertex buffer
		if ( SUCCEEDED( s_mesh.m_pMesh->LockVertexBuffer(D3DLOCK_DISCARD, (LPVOID*)&pVertexInfo) ) )
		{
			// set uv
			//pVertexInfo[4].tu = 0.0f;
			//pVertexInfo[4].tv = 3.0f / 4.0f;

			//pVertexInfo[5].tu = 0.0f;
			//pVertexInfo[5].tv = 0.0f;

			//pVertexInfo[6].tu = 1.0f;
			//pVertexInfo[6].tv = 0.0f;

			//pVertexInfo[7].tu = 1.0f;
			//pVertexInfo[7].tv = 3.0f / 4.0f;

			// unlock
			s_mesh.m_pMesh->UnlockVertexBuffer();
		}

		// create texture
		//hr = D3DXCreateTextureFromFileEx(pd3ddev9, _T(".\\Resource\\map_base.jpg"),
		//	480, 480, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER, 
		//	D3DX_DEFAULT, 0, NULL, NULL, &s_mesh.m_pTextures);
	}
	*/

	// set materials
	s_mesh.m_pMaterials = new D3DXMATERIAL();
	ZeroMemory(s_mesh.m_pMaterials, sizeof(D3DXMATERIAL));
	
	//s_mesh.m_pMaterials->MatD3D.Ambient.a = 1.0f;
	//s_mesh.m_pMaterials->MatD3D.Ambient.r = 0.75f;
	//s_mesh.m_pMaterials->MatD3D.Ambient.g = 0.75f;
	//s_mesh.m_pMaterials->MatD3D.Ambient.b = 0.75f;

	s_mesh.m_pMaterials->MatD3D.Specular.a = 1.0f;
	s_mesh.m_pMaterials->MatD3D.Specular.r = 0.75f;
	s_mesh.m_pMaterials->MatD3D.Specular.g = 0.75f;
	s_mesh.m_pMaterials->MatD3D.Specular.b = 0.75f;

	s_mesh.m_pMaterials->MatD3D.Diffuse.a = 1.0f;
	s_mesh.m_pMaterials->MatD3D.Diffuse.r = 0.5f;
	s_mesh.m_pMaterials->MatD3D.Diffuse.g = 0.5f;
	s_mesh.m_pMaterials->MatD3D.Diffuse.b = 0.5f;

	return hr;
}

Mountain::Mountain(void)
{
}

Mountain::~Mountain(void)
{
}

void Mountain::Init(D3DXMATRIX world, float fHigth/* = 1.0f*/)
{
	D3DXMatrixTranslation(&m_world, 0.0f, 7.0f, 0.0f);
	D3DXMATRIX matScaling;
	D3DXMatrixScaling(&matScaling, 1.0f,fHigth, 1.0f);
	m_world *= matScaling;
	D3DXMatrixMultiply(&m_world, &m_world, &world);
}